#include <bits/stdc++.h>
using namespace std;
class Node{
public:
    int data;
    Node* pNext;
    Node(int d,Node* n =nullptr){
        data = d;
        pNext = n;
    };
};
void show(Node* head)
{
    Node* current = head;
    while(current)
    {
        cout<<current->data<<" ";
        current = current->pNext;
    }
    cout<<"\n";
}
int get_length(Node* a)
{
    Node* current = a;
    int count = 0;
    while(current)
    {
        count++;
        current = current->pNext;
    }

    return count;
}
stack<Node*> put_Node(Node* a)
{
    stack<Node*> result;
    Node* current_a = a;
    while(current_a)
    {
        result.push(current_a);
        current_a = current_a->pNext;
    }
    return result;
}
void show_stack(stack<Node*>& result)
{
    while (result.size()>0)
    {
        cout<<result.top()->data<<" ";
        result.pop();
    }
}
Node* Add_Two(Node* a,Node* A)
{
//    queue<Node*> re;
    stack<Node*> sa = put_Node(a);
    stack<Node*> sb = put_Node(A);

    int min_length = sa.size();
    if(sb.size()<sa.size())
        min_length = sb.size();
    int take_in = 0;
    stack<int> result_num;
    Node* temp_node = sa.top();
    for(int i=0;i<min_length;i++)
    {
        int temp = sa.top()->data+sb.top()->data+take_in;
        if(temp>9)
            take_in = 1;
        else
            take_in = 0;
        int r = temp%10;
        result_num.push(r);
        sa.pop();
        sb.pop();
    }
    while(sa.size()>0)
    {
        int hight_bit;
        if(take_in == 1)
        {
            hight_bit = sa.top()->data+take_in;
            take_in = 0;
        }
        else{
            hight_bit = sa.top()->data;
        }
        result_num.push(hight_bit);
        sa.pop();
    }

    if(result_num.size()>0)
    {
        Node* sum_node = new Node(result_num.top());
        Node* head = sum_node;
        result_num.pop();
        while(result_num.size()>0)
        {
            Node* next_node = new Node(result_num.top());
            head->pNext = next_node;
            head = next_node;
            result_num.pop();
        }
        return sum_node;
    }

}
int main() {
    Node* a = new Node(2);
    Node* b = new Node(8);
    Node* c = new Node(4);
    Node* d = new Node(7);
    Node* e = new Node(1);
    Node* f = new Node(4);
    Node* A = new Node(8);
    Node* B = new Node(9);
    Node* C = new Node(3);
    Node* D = new Node(4);
    Node* E = new Node(2);
    a->pNext = b;
    b->pNext = c;
    c->pNext = d;
    d->pNext = e;
    e->pNext = f;
    A->pNext = B;
    B->pNext = C;
    C->pNext = D;
    D->pNext = E;
    stack<Node*> stack_result = put_Node(a);
    Node* r = Add_Two(a,A);
    Node* current = r;
    while(current)
    {
        cout<<current->data<<" ";
        current = current->pNext;
    }

//    show_stack(stack_result);

//    show(a);
//    show(A);
//    stack<Node*> r = Add_Two(a,A);
//    while(r.size()>0)
//    {
//        cout<<r.top()->data<<" ";
//        cout<<"\n";
//        r.pop();
//    };
    delete a;
    delete b;
    delete c;
    delete d;
    delete e;
    delete f;
    delete A;
    delete B;
    delete C;
    delete D;
    delete E;

    return 0;
}